<script setup>
import { ref,reactive } from 'vue'
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";


const data = reactive({
  form: {
  },
  rules: {
    username: [
      { required: true, message: '请输入账户名', trigger: 'blur' }
    ],
    password:[
      {
        required:true, message: '请输入密码', trigger: 'blur'
      }
    ]
  }
})

const formRef = ref()

const  login = () =>{
  formRef.value.validate((valid) => {
    if (valid){
      request.post("/admin/login",data.form).then(res =>{

        if (res.code === '200') {
          // localStorage.setItem('localdata', res.data)
          localStorage.setItem('localdata', JSON.stringify(res.data || {}))
          ElMessage.success("登陆成功");
          router.push("/")


        } else {
          ElMessage.error(res.msg);

        }
      })
    }
  })

}


</script>

<template>
  <div class="blg">
    <div class="login-container">
      <div style="text-align: center" ><h1>登  陆</h1></div>

      <div class="login-form">
        <el-form ref="formRef" :model="data.form" :rules="data.rules" label-width="80px">
          <el-form-item label="账户名" prop="username">
            <el-input v-model="data.form.username" autocomplete="off" prefix-icon="User"/>

          </el-form-item>


          <el-form-item label="密码" prop="password" >
            <el-input v-model="data.form.password" autocomplete="off" prefix-icon="Lock" show-password/>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">登陆</el-button>
          </el-form-item>

        </el-form>
        <div style="text-align: center;padding-left-left: 10px">
          没有账户？<span ><a href="/register">立即注册</a></span>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>

.blg {
  width: 100%;
  height: 100vh;
  background-image: url("@/assets/imgs/login.jpg");
  background-size: cover;
  background-position: center;
  display: flex;           /* 添加弹性布局 */
  justify-content: center; /* 水平居中 */
  align-items: center;     /* 垂直居中 */
}

.login-container {
  background: rgba(255, 255, 255, 0.8); /* 添加半透明背景 */
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  min-width: 480px;        /* 设置最小宽度 */

}

.login-form {
  width: 100%;
  height: 200px;

}
</style>